package com.lanhai.utils;

import java.util.Arrays;

public class QEncodeUtil {

    public static void main(String args[]){
    	//byte b = 0x35; // 0011 0101
    	byte b = 10;
    	// 输出 [0, 0, 1, 1, 0, 1, 0, 1]
    	System.out.println(Arrays.toString(getBooleanArray(b)));
    	// 输出 00110101
    	System.out.println(byteToBit(b));
    	// JDK自带的方法，会忽略前面的 0
    	System.out.println(Integer.toBinaryString(0x35));
    	//bit转byte
    	String str = byteToBit(b);
    	System.out.println(b);
    	System.out.println(bitToByte(str));
    }
	
	/** 
     * 将byte转换为一个长度为8的byte数组，数组每个值代表bit 
     */  
    public static byte[] getBooleanArray(byte b) {  
        byte[] array = new byte[8];  
        for (int i = 7; i >= 0; i--) {  
            array[i] = (byte)(b & 1);  
            b = (byte) (b >> 1);  
        }  
        return array;  
    }  
    
    /** 
     * 把byte转为字符串的bit 
     */  
    public static String byteToBit(byte b) {  
        return ""  
                + (byte) ((b >> 7) & 0x1) + (byte) ((b >> 6) & 0x1)  
                + (byte) ((b >> 5) & 0x1) + (byte) ((b >> 4) & 0x1)  
                + (byte) ((b >> 3) & 0x1) + (byte) ((b >> 2) & 0x1)  
                + (byte) ((b >> 1) & 0x1) + (byte) ((b >> 0) & 0x1);  
    }

    /** 
     * 把bit字符串转成byte 
     */ 
    public static byte bitToByte(String bit) {  
        int re, len;  
        if (null == bit) {  
            return 0;  
        }  
        len = bit.length();  
        if (len != 4 && len != 8) {  
            return 0;  
        }  
        if (len == 8) {// 8 bit处理  
            if (bit.charAt(0) == '0') {// 正数  
                re = Integer.parseInt(bit, 2);  
            } else {// 负数  
                re = Integer.parseInt(bit, 2) - 256;  
            }  
        } else {//4 bit处理  
            re = Integer.parseInt(bit, 2);  
        }  
        return (byte) re;  
    }    
	
}
